/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.model.listener;

import android.os.SystemClock;
import android.view.View;


/**
 * Created by geshimin on 18/8/7.
 */
public abstract class NoDoubleClickListener implements View.OnClickListener {
    private int MIN_CLICK_DELAY_TIME = 600;
    private long lastClickTime = 0;
    private int viewId = -1;

    public final static int NORMAL_SHORT_TIME = 250;

    public NoDoubleClickListener(){

    }

    public NoDoubleClickListener(int delayTime) {
        this.MIN_CLICK_DELAY_TIME = delayTime;
    }

    @Override
    public void onClick(final View v) {
        long currentTime = SystemClock.elapsedRealtime();
        if (v.getId() == viewId) {
            if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
                handleClick(currentTime, v);
            }
        } else {
            viewId = v.getId();
            handleClick(currentTime, v);
        }
    }

    private void handleClick(long currentTime, final View view) {
        lastClickTime = currentTime;
        onNoDoubleClick(view);
    }

    public abstract void onNoDoubleClick(View view);
}
